package m.app.action;

import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.store.model.GoodsStockRecord;
import m.store.model.OrderModel;
import m.store.model.StockInfo;
import m.store.model.StoreGoods;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentParamMeta;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

@ActionMeta(name = "app/goodsStockRecord",title = "app入库明细")
public class GoodsStockRecordAction extends AppAction{
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "库存查询列表",permission = true),
        params = {
            @DocumentParamMeta(name = "page.index",description = "分页开始位置",type = FieldType.INT),
            @DocumentParamMeta(name = "page.num",description = "分页每页数量",type = FieldType.INT),
            @DocumentParamMeta(name = "params[virtualTab]",description = "虚拟库存",type = FieldType.STRING,length = 50,dictOption = "A:正常库存,B:虚拟库存"),
            @DocumentParamMeta(name = "params[oid@eq]",description = "条件示例，字段和操作符中间用@，值为空不添加",type = FieldType.STRING,length = 50),
            @DocumentParamMeta(name = "params[oid#desc]",description = "排序示例，字段和操作符中间用#，值为空不生效",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage queryList() throws Exception {
        String virtualTab=getParamsValue("virtualTab");
        ModelQueryList queryList=toQueryList(GoodsStockRecord.class,"*","goods.*","goods.storeType.name","stock.*","stock.guest.name");
        if(StringUtil.noSpace(virtualTab).equals("C")) {
            queryList.addCondition(QueryCondition.eq("stock.type", StockInfo.Type_Server));
            queryList.addCondition(QueryCondition.eq("stock.status", OrderModel.Status_Done));
            queryList.addOrder(
                QueryOrder.asc("goods.storeType.oid"),
                QueryOrder.asc("goods.typeName")
            );
        }else if(StringUtil.noSpace(virtualTab).equals("B")){
            queryList.addCondition(QueryCondition.eq("stock.type", StockInfo.Type_Virtual));
            queryList.addCondition(QueryCondition.eq("stock.status", OrderModel.Status_Register));
            queryList.addOrder(
                QueryOrder.asc("goods.storeType.oid"),
                QueryOrder.asc("stock.guest.name"),
                QueryOrder.asc("goods.typeName")
            );
        }else{
            queryList.addCondition(QueryCondition.gt("surplusNum", 0));
            queryList.addCondition(QueryCondition.eq("stock.status",OrderModel.Status_Done));
            queryList.addOrder(
                QueryOrder.asc("goods.storeType.sort"),
                QueryOrder.asc("repository.oid"),
                QueryOrder.asc("stock.guest.name"),
                QueryOrder.asc("goods.name")
            );
        }
        queryList.addCondition(QueryCondition.eq("store.oid", PowerUtil.getSessionStoreOid()));
        return JSONMessage.success("查询成功")
                .push("page",ModelQueryList.getModelPageInfo(queryList));
    }
}
